/*
 * Copyright 2011 The Rusted Roof, LLC.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package org.proteusframework.core.runtime;

import org.proteusframework.core.api.IProteusConstants;
import org.proteusframework.core.base.AbstractRuntimeConfiguration;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Provides a default runtime implementation of the Proteus Framework. Most applications can simply rely on the default
 * runtime configuration.
 *
 * @author Tacoma Four
 */
public class DefaultRuntimeConfiguration extends AbstractRuntimeConfiguration
{
    /**
     * Logger.
     */
    private static final Logger logger = Logger.getLogger(DefaultRuntimeConfiguration.class.getCanonicalName());

    /**
     * Properties containing the actual metadata used to build the runtime configuration.
     */
    protected Properties proteusProperties = new Properties();

    /**
     * {@inheritDoc}
     */
    @Override
    protected void onDelegateInjected()
    {
        if (logger.isLoggable(Level.FINEST))
        {
            logger.finest("Delegate injected into DefaultRuntimeConfiguration");
        }


        // Locate the proteus.properties file on the CLASSPATH and read its values into the

        if (logger.isLoggable(Level.FINEST))
        {
            logger.finest("Attempting to load /proteus.properties from the CLASSPATH");
        }

        InputStream inputStream = getClass().getResourceAsStream(IProteusConstants.PROTEUS_BOOTSTRAP_PROPERTIES);

        if (null == inputStream)
        {
            logger.severe("Unable to locate /proteus.properties on the CLASSPATH");
        } else
        {
            try
            {
                proteusProperties.load(inputStream);

                String msg = "Located the /proteus.properties configuration file ("
                        + proteusProperties.size()
                        + " entries)";

                logger.config(msg);

                if (logger.isLoggable(Level.FINE))
                {
                    logger.fine("Located a total of " + proteusProperties.size() +
                            " configuration properties in /proteus.properties");

                    for (String key : proteusProperties.stringPropertyNames())
                    {
                        logger.fine(key + " :: " + proteusProperties.getProperty(key));
                    }
                }

            } catch (IOException e)
            {
                logger.severe("Exception reading /proteus.properties from the CLASSPATH: " + e.getMessage());
            }
        }
    }
}
